build: disable Bun autoload of .env and bunfig.toml in compiled CLI#808
Merged
build: disable Bun autoload of .env and bunfig.toml in compiled CLI#808
Conversation
Without these flags, Bun silently loads `.env` / `.env.local` / `.env.production` and `bunfig.toml` from the user's CWD into `process.env` before any of our code runs. That's dangerous for a global CLI: - `SENTRY_AUTH_TOKEN` in a project's `.env.local` would override the stored OAuth token via `getRawEnvToken()` in `src/lib/db/auth.ts`. - `SENTRY_ORG` / `SENTRY_PROJECT` / `SENTRY_DSN` would pre-empt our DSN auto-detection flow in `src/lib/resolve-target.ts`. - Any other `SENTRY_*` var from `src/lib/env-registry.ts` (30+ of them) set in a project `.env` would leak into the CLI with no indication. Verified via strace: baseline binary opens `bunfig.toml` + `.env` before our code runs; patched binary does not. End-to-end: baseline picked up `SENTRY_ORG=injected-from-dotenv` from a hostile `.env` and queried it; patched correctly ignored it and fell through to auto-detect. Shell-level env vars continue to work unchanged. Users who want directory- scoped env vars should use `direnv` or source their `.env` explicitly. Also leaves a NOTE about `bytecode: true`: it would move JS parse cost from startup to build time, but as of Bun 1.3.11 it crashes our ESM bundle at runtime with "Expected CommonJS module to have a function wrapper" before any of our code runs (likely related to oven-sh/bun#21097 / #23490). Revisit once Bun's bytecode path stabilizes for our two-step esbuild-then-compile pipeline.
Contributor
Codecov Results 📊✅ 138 passed | Total: 138 | Pass Rate: 100% | Execution Time: 0ms 📊 Comparison with Base Branch
✨ No test changes detected All tests are passing successfully. ✅ Patch coverage is 100.00%. Project has 1769 uncovered lines. Coverage diff@@ Coverage Diff @@
## main #PR +/-##
==========================================
+ Coverage 95.63% 95.63% —%
==========================================
Files 281 281 —
Lines 40462 40462 —
Branches 0 0 —
==========================================
+ Hits 38692 38693 +1
- Misses 1770 1769 -1
- Partials 0 0 —Generated by Codecov Action |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Pass
autoloadDotenv: falseandautoloadBunfig: falsetoBun.build({ compile })inscript/build.tsso the compiledsentrybinary no longer silently reads.env/.env.local/.env.production/bunfig.tomlfrom the user's CWD at startup.Why this matters
Without these flags, Bun auto-loads those files into
process.envbefore our code runs. For a globally-installed CLI that users invoke from arbitrary project directories, this is a real footgun:SENTRY_AUTH_TOKENin a Next.js project's.env.localwould override the stored OAuth token viagetRawEnvToken()insrc/lib/db/auth.ts.SENTRY_ORG/SENTRY_PROJECT/SENTRY_DSNwould pre-empt our DSN auto-detection insrc/lib/resolve-target.ts.SENTRY_*var fromsrc/lib/env-registry.ts(30+ of them) set in a project.envwould leak into the CLI with no indication to the user.Shell-level env vars continue to work unchanged. Users who want directory-scoped env vars should use
direnvor source their.envexplicitly — matching howgh,docker, etc. behave.Verification
strace evidence (before/after with a hostile
.env+bunfig.tomlin CWD):openat("bunfig.toml")openat(".env").env*walkSENTRY_DSN=URLs, not for runtime injection)End-to-end behavior with
.envcontainingSENTRY_ORG=injected-from-dotenv,SENTRY_PROJECT=injected-from-dotenv:.env, fell through to auto-detect and returned the usual "Could not auto-detect organization and project" hint.Size: identical byte-for-byte to baseline (105,720,476 raw / 31,580,634 gzipped). The flags are runtime-only — they don't affect bundled content.
Startup (
hyperfine --warmup 5 --runs 30 'sentry --version'):Within noise — the win here is determinism and safety, not speed. Avoiding two
openatsyscalls at startup doesn't move the needle when the JS parse dominates.What about
bytecode: true?Also explored as part of this work but not landed. Bytecode compilation would move JS parse cost from user-visible startup to build time (Bun docs report ~2× startup wins for
tsc-scale apps). Unfortunately, as of Bun 1.3.11,bytecode: truecrashes our ESM bundle at runtime:The error fires before any of our code runs (stack points at Bun's module loader). Binary size grew only ~9 KB raw / ~1.3 KB gzipped, suggesting bytecode isn't even being embedded correctly — the flag just flips an assertion that trips the loader. Likely interacts with our two-step pipeline (esbuild → ESM bundle →
Bun.buildcompile) that's unusual among bytecode use cases.Closest-matching upstream issues (neither a clean match):
import.meta.envcauses a cryptic TypeError inbun build --compile" (open)Left a
NOTEcomment incompileTarget()so the next person doesn't re-try it without checking Bun's release notes first.